package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.health.commons.PropertiesService;
import com.health.db.dto.DoctorListDto;
import com.health.db.dto.RecomDocDto;
import com.health.db.model.AdvertisementList;
import com.health.db.model.AdvertisementListExample;
import com.health.db.model.AdvertisementListTemp;
import com.health.db.model.GoodsPriceListTemp;
import com.health.db.model.UserLIST;
import com.health.db.model.UserLoginJournal;
import com.health.db.model.VoiceOnDemondExample;
import com.health.db.model.ZAdviceHotExample;
import com.health.db.service.msg.IUserLoginJournalService;
import com.health.db.service.order.IZAdviceHotService;
import com.health.db.service.order.IZAdviceListService;
import com.health.db.service.sm.IAdvertisementListService;
import com.health.db.service.sm.IDepartmentListService;
import com.health.db.service.user.IGoodsPriceListService;
import com.health.db.service.user.IStuffRecommondService;
import com.health.db.service.user.IUserLISTService;
import com.health.db.service.user.IVoiceOnDemondService;
import com.health.db.service.user.IZDocSortListService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.InitReq;
import com.health.mapp.bean.InitResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.JudeMobileAttribution;
import com.health.mapp.utils.Response;
/**
 *  初始化
 *
 */
public class InitImpl implements ActionInterface {

	private IAdvertisementListService advertisementListService  ;
	private IDepartmentListService departmentListService;
	private IZAdviceListService zAdviceListService;
	private IZDocSortListService zDocSortListService;
	private IGoodsPriceListService goodsPriceListService  ;
	private IStuffRecommondService stuffRecommondService;
	
	private IUserLoginJournalService userLoginJournalService;
	
	private IUserLISTService userLISTService;
	
	private IVoiceOnDemondService voiceOnDemondService;
	
	private IZAdviceHotService zAdviceHotService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		InitReq req = (InitReq) reqBean;
		InitResp _resp = new InitResp();
		
		Long userId = req.getUserId();
		
		//当传入的userId是空的时候
		if (null==userId||0==userId.intValue()) {
			UserLoginJournal userLoginJournal =userLoginJournalService.selectByMachinedeviceid(header.getUuId());
			
			
			if (null!=userLoginJournal) {
				userId = userLoginJournal.getUserid();
			}
		}
		
		List<GoodsPriceListTemp> initPriceListReturn =  new ArrayList<GoodsPriceListTemp>();
		
		//所有患者付款公共的价格
		List<GoodsPriceListTemp> initPriceList =goodsPriceListService.selectByDocIdForUserUsed(null);
		
		Map<String,GoodsPriceListTemp> initPriceListMap = new HashMap<String,GoodsPriceListTemp>();
		
		  if (userId != null) {
	            UserLIST userLIST= userLISTService.selectByUserId(userId);
	            if (null!=userLIST&&Constant.CONSTANTS_FREE_PROVICE_NAME.equals(JudeMobileAttribution.getMobileAttribution(userLIST.getUserNo()))) {
	            	
	            	 for (int i = 0; i < initPriceList.size(); i++) {
	            		 GoodsPriceListTemp temp =initPriceList.get(i);
	            		 if (temp.getGoodsId().equals("8")) {
	            			 temp.setPrice("0.00");
	            			 initPriceListMap.put(temp.getGoodsId(), temp);
						}
					}
				}else{
					for (int i = 0; i < initPriceList.size(); i++) {
		    			
		    			GoodsPriceListTemp temp =initPriceList.get(i);
		    			initPriceListMap.put(temp.getGoodsId(), temp);
		    		}
				}
	        }else{
	        	for (int i = 0; i < initPriceList.size(); i++) {
	    			
	    			GoodsPriceListTemp temp =initPriceList.get(i);
	    			initPriceListMap.put(temp.getGoodsId(), temp);
	    		}
	        }
		
		String[] initPriceIds = PropertiesService.getString("initPriceId").trim().split(",");
		
		for (int i = 0; i < initPriceIds.length; i++) {
			String goodsId =initPriceIds[i];
			initPriceListReturn.add(initPriceListMap.get(goodsId));
		}
		//塞入筛选后的价格列表
		_resp.setInitPriceList(initPriceListReturn);
		
		//头部广告
		Integer topPostionId = 1;
		
		AdvertisementListExample topPostionExample = new  AdvertisementListExample();
		topPostionExample.setPositionid(Long.valueOf(topPostionId));
		topPostionExample.setOffset(0);
		topPostionExample.setPageSize(6);
		
		_resp.setTopBannerList(advertisementListService.selectSimple(topPostionExample));
		
		AdvertisementListExample mainostionExample = new  AdvertisementListExample();
		mainostionExample.setPositionid(Long.valueOf(2));
		
		mainostionExample.setOffset(0);
		mainostionExample.setPageSize(4);
		List<AdvertisementListTemp> mainAdvice= advertisementListService.selectSimple(mainostionExample);
		
		List<AdvertisementList> mainAdviceResp= new ArrayList<AdvertisementList>();
		
		for (int i = 0; i < mainAdvice.size(); i++) {
			AdvertisementListTemp temp = mainAdvice.get(i);
			AdvertisementList re = new AdvertisementList();
			re.setAdvertisementDesc(temp.getAdvertisementDesc());
			re.setAdvertisementName(temp.getBannerName());
			re.setAdvertiseTag(temp.getAdvertiseTag());
			re.setLikedAmount(temp.getLikedAmount());
			re.setReadedAmount(temp.getReadedAmount());
			re.setAdvertSort(temp.getBannerOrder());
			re.setAdvertisementPictureUrl(temp.getBannerPic());
			re.setClickLinkUrl(temp.getBannerUrl());
			re.setAdvertisementid(temp.getBannerId());
			re.setShareLinkUrl(temp.getShareLinkUrl());
			re.setCommon(temp.getCommon());
			mainAdviceResp.add(re);
		}
		
		
		AdvertisementListExample mainostionExample4 = new  AdvertisementListExample();
		mainostionExample4.setPositionid(Long.valueOf(4));
		
		mainostionExample4.setOffset(0);
		mainostionExample4.setPageSize(4);
		List<AdvertisementListTemp> mainAdvice4= advertisementListService.selectSimple(mainostionExample4);
		List<AdvertisementList> mainAdviceResp4= new ArrayList<AdvertisementList>();
		
		for (int i = 0; i < mainAdvice4.size(); i++) {
			AdvertisementListTemp temp = mainAdvice4.get(i);
			AdvertisementList re = new AdvertisementList();
			re.setAdvertisementDesc(temp.getAdvertisementDesc());
			re.setAdvertisementName(temp.getBannerName());
			re.setAdvertiseTag(temp.getAdvertiseTag());
			re.setLikedAmount(temp.getLikedAmount());
			re.setReadedAmount(temp.getReadedAmount());
			re.setAdvertSort(temp.getBannerOrder());
			re.setAdvertisementPictureUrl(temp.getBannerPic());
			re.setClickLinkUrl(temp.getBannerUrl());
			re.setAdvertisementid(temp.getBannerId());
			re.setShareLinkUrl(temp.getShareLinkUrl());
			re.setCommon(temp.getCommon());
			mainAdviceResp4.add(re);
		}
		
		_resp.setMainAdvice(mainAdviceResp);
		_resp.setMainAdvice4(mainAdviceResp4);
		
		
		ZAdviceHotExample zAdviceHotExample = new ZAdviceHotExample();
		zAdviceHotExample.setIsHotFlag((short) 1);
		_resp.setHotAdviceNew(zAdviceHotService.selectSimple(zAdviceHotExample));
		
		_resp.setVoiceOnDemond(voiceOnDemondService.selectBySimple(new VoiceOnDemondExample()));
		//科室
		_resp.setDepartmentList(departmentListService.selectSimpleForInit());
		_resp.setHotAdvice(zAdviceListService.selectSimpleForInit());
		_resp.setDocSortList(zDocSortListService.selectSimpleForInit());
		
		List<DoctorListDto> docRecomList =stuffRecommondService.selectAllRecommond();
		RecomDocDto dto = new RecomDocDto();
		if (docRecomList.get(0).getDocId().equals("0")) {
			dto.setDocRecomTitle(docRecomList.get(0).getDocName());
			docRecomList.remove(0);
			dto.setDocRecomList(docRecomList);
		}else{
			dto.setDocRecomTitle("推荐医生");
			dto.setDocRecomList(docRecomList);
		}
		
		_resp.setDocRecommend(dto);
		return _resp;
	}
	
	
	/**
	 * @return the zDocSortListService
	 */
	public IZDocSortListService getzDocSortListService() {
		return zDocSortListService;
	}


	/**
	 * @param zDocSortListService the zDocSortListService to set
	 */
	public void setzDocSortListService(IZDocSortListService zDocSortListService) {
		this.zDocSortListService = zDocSortListService;
	}


	/**
	 * @return the advertisementListService
	 */
	public IAdvertisementListService getAdvertisementListService() {
		return advertisementListService;
	}
	/**
	 * @param advertisementListService the advertisementListService to set
	 */
	public void setAdvertisementListService(
			IAdvertisementListService advertisementListService) {
		this.advertisementListService = advertisementListService;
	}
	/**
	 * @return the departmentListService
	 */
	public IDepartmentListService getDepartmentListService() {
		return departmentListService;
	}
	/**
	 * @param departmentListService the departmentListService to set
	 */
	public void setDepartmentListService(
			IDepartmentListService departmentListService) {
		this.departmentListService = departmentListService;
	}
	/**
	 * @return the zAdviceListService
	 */
	public IZAdviceListService getzAdviceListService() {
		return zAdviceListService;
	}
	/**
	 * @param zAdviceListService the zAdviceListService to set
	 */
	public void setzAdviceListService(IZAdviceListService zAdviceListService) {
		this.zAdviceListService = zAdviceListService;
	}


	/**
	 * @return the goodsPriceListService
	 */
	public IGoodsPriceListService getGoodsPriceListService() {
		return goodsPriceListService;
	}


	/**
	 * @param goodsPriceListService the goodsPriceListService to set
	 */
	public void setGoodsPriceListService(
			IGoodsPriceListService goodsPriceListService) {
		this.goodsPriceListService = goodsPriceListService;
	}


	public IStuffRecommondService getStuffRecommondService() {
		return stuffRecommondService;
	}


	public void setStuffRecommondService(
			IStuffRecommondService stuffRecommondService) {
		this.stuffRecommondService = stuffRecommondService;
	}


	public IUserLoginJournalService getUserLoginJournalService() {
		return userLoginJournalService;
	}


	public void setUserLoginJournalService(
			IUserLoginJournalService userLoginJournalService) {
		this.userLoginJournalService = userLoginJournalService;
	}


	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}


	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}


	public IVoiceOnDemondService getVoiceOnDemondService() {
		return voiceOnDemondService;
	}


	public void setVoiceOnDemondService(IVoiceOnDemondService voiceOnDemondService) {
		this.voiceOnDemondService = voiceOnDemondService;
	}


	public IZAdviceHotService getzAdviceHotService() {
		return zAdviceHotService;
	}

	public void setzAdviceHotService(IZAdviceHotService zAdviceHotService) {
		this.zAdviceHotService = zAdviceHotService;
	}
}
